package com.yeqifu.bus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeqifu.bus.entity.Goods;
import com.yeqifu.bus.entity.Sales;
import com.yeqifu.bus.mapper.GoodsMapper;
import com.yeqifu.bus.mapper.InportMapper;
import com.yeqifu.bus.mapper.SalesMapper;
import com.yeqifu.bus.mapper.xml.InportMapperCustom;
import com.yeqifu.bus.mapper.xml.SalesMapperCustom;
import com.yeqifu.bus.service.ISalesService;
import com.yeqifu.bus.vo.AnountVO;
import com.yeqifu.bus.vo.InportVOS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 * InnoDB free: 9216 kB 服务实现类
 * </p>
 *
 * @author luoyi-
 * @since 2019-12-21
 */
@Service
public class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements ISalesService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private SalesMapperCustom salesMapper;

    @Autowired
    private InportMapperCustom inportMapperCustom;

    /**
     * 添加商品销售
     *
     * @param entity 商品销售实体类
     * @return
     */
    @Override
    public boolean save(Sales entity) {
        Goods goods = goodsMapper.selectById(entity.getGoodsid());
        goods.setNumber(goods.getNumber() - entity.getNumber());
        //更新商品的库存信息
        goodsMapper.updateById(goods);
        return super.save(entity);
    }

    /**
     * 更新商品销售
     *
     * @param entity 商品销售实体类
     * @return
     */
    @Override
    public boolean updateById(Sales entity) {
        //根据销售单ID查询销售单信息
        Sales sales = baseMapper.selectById(entity.getId());
        Goods goods = goodsMapper.selectById(entity.getGoodsid());
        //仓库商品数量=原库存-销售单修改之前的数量+修改之后的数量
        //     80  +40 -  50     30
        goods.setNumber(goods.getNumber() + sales.getNumber() - entity.getNumber());
        //更新商品
        goodsMapper.updateById(goods);
        return super.updateById(entity);
    }

    /**
     * 删除商品销售信息
     *
     * @param id 商品销售单ID
     * @return
     */
    @Override
    public boolean removeById(Serializable id) {
        //根据商品销售单ID查询出销售单数据
        Sales sales = baseMapper.selectById(id);
        Goods goods = goodsMapper.selectById(sales.getGoodsid());
        //仓库商品数量=原库存+删除商品销售单的数量
        goods.setNumber(goods.getNumber() + sales.getNumber());
        goodsMapper.updateById(goods);
        return super.removeById(id);
    }

    /**
     * 查询当前总金额
     *
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Float getAmount() {
        List<AnountVO> sales = salesMapper.selectAll();
        // 使用 BigDecimal 构建,以免缺失精度
        BigDecimal count = null;
        for (int i = 0; i < sales.size(); i++) {
            AnountVO anountVO = sales.get(i);
            BigDecimal salePrice = new BigDecimal(String.valueOf(anountVO.getSalePrice()));
            BigDecimal number = new BigDecimal(String.valueOf(anountVO.getNumber()));
            if (count == null) {
                count = salePrice.multiply(number);
            } else {
                count = salePrice.multiply(number).add(count);
            }
        }
        return count.floatValue();
    }

    @Override
    public Float getInportAmount() {
         List<InportVOS> inportVOS =  inportMapperCustom.selectAll();
        // 使用 BigDecimal 构建,以免缺失精度
        BigDecimal count = null;
        for (int i = 0; i < inportVOS.size(); i++) {
            InportVOS inportVOS1 = inportVOS.get(i);
            BigDecimal salePrice = new BigDecimal(String.valueOf(inportVOS1.getInportPrice()));
            BigDecimal number = new BigDecimal(String.valueOf(inportVOS1.getNumber()));
            if (count == null) {
                count = salePrice.multiply(number);
            } else {
                count = salePrice.multiply(number).add(count);
            }
        }
        return count.floatValue();
    }
}
